<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
include_once('Admin.php');
@set_time_limit(0);
$DataDir = "../data/";
if(!empty($_GET['Action'])){
	include_once('../class/mysqlbackup.class.php');
	$config = array(
		'host' => $dbhost,
		'port' => 3306,
		'userName' => $dbuser,
		'userPassword' => $dbpass,
		'dbprefix' => $dbprefix,
		'charset' => 'UTF8',
		'path' => $DataDir,
		'isCompress' => 0, //是否开启gzip压缩
		'isDownload' => 0
	);
	$mr = new MySQLReback($config);	
	$mr -> setDBName($dbname);
	if($_GET['Action'] == 'backup'){
		$mr -> backup();
		AdminLog(6,1,'数据库备份操作，结果：操作成功！');
		MsgBox (1,'数据库备份成功！','0');		
	} elseif ($_GET['Action'] == 'RL'){
		$mr -> recover($_GET['File']);
		AdminLog(6,1,'数据库还原操作，结果：操作成功！');
		MsgBox (1,'数据库还原成功！','0');		
	} elseif ($_GET['Action'] == 'Del'){
		if(@unlink($DataDir.$_GET['File'])){
			AdminLog(3,1,'删除数据库备份文件，结果：操作成功！');
			MsgBox (0,'','0');			
		}else{
			AdminLog(3,1,'删除数据库备份文件，结果：操作失败！');
			MsgBox (1,'删除失败！','0');			
		}
	}
	if($_GET['Action'] == 'dow'){
		function DownloadFile($fileName) {
			ob_end_clean();
			header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
			header('Content-Description: File Transfer');
			header('Content-Type: application/octet-stream');
			header('Content-Length: ' . filesize($fileName));
			header('Content-Disposition: attachment; filename=' . basename($fileName));
			readfile($fileName);
		}
		DownloadFile($DataDir.$_GET['file']);
		exit();
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="./images/adminstyle.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="../favicon.ico">
<link rel="Bookmark" href="../favicon.ico">
<script src="../js/csshovernotie6.js" type="text/javascript"></script>
</head>
<body>
<br />
<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <td colspan="4" align="center" class="Main_Title">数据库备份与还原</td>
  </tr>
  <tr class="odd">
    <td height="20" align="center" class="b">文件名</td>
    <td align="center" class="b">备份时间</td>
    <td align="center" class="b">文件大小</td>
    <td align="center" class="b">管理选项</td>
  </tr>
<?php

$FileArr = MyScandir($DataDir);
foreach ($FileArr as $i => $n){
	if($n != 'PHPMyAdminInitialData.sql' && $i>1){
		$FileTime = date('Y-m-d H:i:s',filemtime($DataDir . $n));
		$FileSize = filesize($DataDir . $n)/1024; //KB
		
		if ($FileSize < 1024){
			$FileSize = number_format($FileSize,2) . ' KB';
		} else {
			$FileSize = '<font color="#FF0000">' . number_format($FileSize/1024,2) . '</font> MB';
		}
		$sAS = "<a href=\"?Action=dow&file=" . $n . "\">下载</a> | ";
		$sAS .= "<a onClick=\"return confirm('确定将数据库还原到当前备份吗？');\" href=\"?Action=RL&File={$n}\">还原</a> | ";
		$sAS .= "<a onClick=\"return confirm('确定删除该备份文件吗？');\" href=\"?Action=Del&File={$n}\">删除</a>";
		echo "<tr class=\"odd\">
		<td height=\"20\" align=\"center\">{$n}</td>
		<td align=\"center\">{$FileTime}</td>
		<td align=\"center\">{$FileSize}</td>
		<td align=\"center\">{$sAS}</td>
		</tr>";
		unset($FileTime,$FileSize,$sAS);
	}
}
?>  
  <tr class="odd">
    <td colspan="4" height="40" align="center"><input type="button" value="备份数据库" style="width:120px; height:28px;" onClick="javascript:location.href='MySQLBackup.php?Action=backup'"></td>
  </tr>
</table>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="6">
	<tr class="Notodd">
		<td style="color:#333333; line-height:20px;">
			注：1、本操作只对数据库中当前网站数据(表前缀为 "<?php echo $dbprefix;?>" 的表)进行备份。如果您的数据库中有多个网站，其它站点不受影响。<br />
			　　2、备份后的数据可以进行还原操作或通过 phpMyAdmin 导入。
		</td>
	</tr>
</table> 
</body>
</html>
